package com.demo.service.impl;

import com.demo.dto.*;
import com.demo.mapper.KsClassMapper;
import com.demo.mapper.UserMapper;
import com.demo.service.UserService;
import com.demo.utils.ResponseResult;
import com.demo.pojo.KsClass;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: TLB
 * @Date: 2022/11/10 20:01
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private KsClassMapper ksClassMapper;
    @Override
    public ResponseResult login(LoginDto loginDto) {
          User user=  userMapper.selectByLogDto(loginDto);
        if (user==null) {
            return ResponseResult.error("用户名,密码或所属班级错误");
        }
          return ResponseResult.success(user);

    }

    @Override
    public ResponseResult findRoleByName(String name) {
        User user = userMapper.selectByMobile(name);

        if (user==null){
            return ResponseResult.error("账户不存在");
        }
        //是老师
        if (user.getRoleId()==1){
            List<KsClass> classes = ksClassMapper.selectAll();
            return ResponseResult.success(classes);
        }
        //是学生
        List<KsClass> classes=ksClassMapper.selectById(user.getClassId());
        return ResponseResult.success(classes);
    }

    @Override
    public ResponseResult findByClassId(Integer classId) {
        List<User> users=userMapper.selectByClassId(classId);
        return ResponseResult.success(users);
    }

    @Override
    public ResponseResult addIntegral(AddIntegralDto dto) {

        int i=userMapper.updateIntegralById(dto);
        return ResponseResult.success(dto.getIntegral());
    }

    @Override
    public ResponseResult showWorks(ShowWorksDto dto) {
        List<UserWorksDto> userWorksDtos=userMapper.showWorks(dto);
        return ResponseResult.success(userWorksDtos);
    }

    @Override
    public ResponseResult resetGScoreByGroupId(Integer classId) {
        int row=userMapper.updateIntegralByGroupId(classId);
        return ResponseResult.success("重置成功");
    }


}
